package 程序员面试金典;

import java.util.Arrays;
import java.util.LinkedList;

public class TripleInOne {
    private int[] arr = {};
    private int[] size = {};
    private int stackSize;
    public TripleInOne(int stackSize) {
        this.stackSize = stackSize;
        // 第一个栈 [0*stackSize, 1*stackSize)，第二个栈 [1*stackSize, 2*stackSize)，第三个栈 [2*stackSize, 3*stackSize)
        arr = new int[3*stackSize];
        size = new int[] {0, 0, 0};
    }

    public void push(int stackNum, int value) {
        int length = size[stackNum];
        if (length < stackSize) {
            arr[stackNum*stackSize+length] = value;
            size[stackNum] = length + 1;
        }
    }

    public int pop(int stackNum) {
        int topVal = peek(stackNum);
        if (size[stackNum] > 0)
            size[stackNum] -= 1;
        return topVal;
    }

    public int peek(int stackNum) {
        if (size[stackNum] == 0)
            return -1;
        return arr[stackNum*stackSize+size[stackNum]-1];
    }

    public boolean isEmpty(int stackNum) {
        return size[stackNum] == 0;
    }
//    int[] cur;
//    boolean[] booleans;
//    public TripleInOne(int stackSize) {
//      cur=new int[stackSize];
//      booleans=new boolean[stackSize];
//    }
//
//    public void push(int stackNum, int value) {
//        if (!booleans[stackNum]){
//         cur[stackNum]=value;
//         booleans[stackNum]=true;
//        }
//    }
//
//    public int pop(int stackNum) {
//        //为true，代表里面有值
//        if (booleans[stackNum]){
//            int num = cur[stackNum];
//            booleans[stackNum]=false;
//            cur[stackNum]=-1;
//            return num;
//        }
//        return -1;
//    }
//
//    public int peek(int stackNum) {
//        if (booleans[stackNum]){
//            booleans[stackNum]=false;
//            return cur[stackNum];
//        }
//        return -1;
//    }
//
//    public boolean isEmpty(int stackNum) {
//          return !booleans[stackNum];
//    }
}
